package java_learn.java_web.基础增强.反射;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * Class对象功能：
 * 获取功能：
 * 1. 获取成员变量们
 * Field[] getFields()
 * Field getField(String name)
 * <p>
 * Field[] getDeclaredFields()
 * Field getDeclaredField(String name)
 * 2. 获取构造方法们
 * Constructor<?>[] getConstructors()
 * Constructor<T> getConstructor(类<?>... parameterTypes)
 * <p>
 * Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
 * Constructor<?>[] getDeclaredConstructors()
 * 3. 获取成员方法们：
 * Method[] getMethods()
 * Method getMethod(String name, 类<?>... parameterTypes)
 * <p>
 * Method[] getDeclaredMethods()
 * Method getDeclaredMethod(String name, 类<?>... parameterTypes)
 * <p>
 * 4. 获取类名
 * String getName()
 *
 * @Author: 李浩然
 * @Date: 2021/5/24 11:53 上午
 */
public class ReflectDemo04 {
    public static void main(String[] args) throws Exception {
        // * Method[] getMethods()
        Class person = Person.class;
        Method eat_method = person.getMethod("eat");
        Person p = new Person();
        eat_method.invoke(p);

        Method eat_method2 = person.getMethod("eat", String.class);
        eat_method2.invoke(p, "饭");

        Method[] methods = person.getMethods();
        for (Method method : methods) {
            System.out.println(method);
            method.setAccessible(true);
            String name = method.getName();
            System.out.println(name);
        }

        String className = person.getName();
        System.out.println(className);
    }
}
